$(function () {
    let laypage = layui.laypage;
    let params = {
        key:'',
        type:'',
        state:'',
        page:1,
        perpage:10
    }
    getArticle_list()
    function getArticle_list() {
        $.ajax({
            url:'http://localhost:8080/api/v1/admin/article/query',
            headers:{
                Authorization:localStorage.getItem('token')
            },
            data:params,
            success(res) {
                // console.log(res);
                if (res.code === 200) {
                    // console.log(res.data);
                    let list = res.data.data
                    // console.log(list);
                    let html = template('listTpl',{list})
                    $('#articleTbale tbody').html(html)
                    laypage.render({
                        elem: 'pager' //注意，这里的 test1 是 ID，不用加 # 号
                        ,count: res.data.totalCount //数据总数，从服务端得到
                        ,curr:params.page
                        ,jump: function(obj, first){
                            if(!first){
                              params.page = obj.curr
                              getArticle_list()
                            }
                        }
                    });
                } else {
                    console.log(res.msg);
                }
            }
        })
    }
    
    $('#btnSearch').click(function () {
        let aname = $('#aname').val().trim()
        let selCategory  = $('#selCategory').val()
        // console.log($('#selCategory').val());
        let selStatus  = $('#selStatus').val()
        params.key = aname
        params.type = selCategory
        params.state = selStatus
        params.page = 1
        getArticle_list()
    })
    $('#reset').click(function() {
        $('#aname').val('')
        $('#selStatus').val('')
        params = {
            key:'',
            type:'',
            state:'',
            page:1,
            perpage:10
        }
        getArticle_list()
    })

    function getAllList() {
        $.ajax({
            url:'http://localhost:8080/api/v1/admin/category/list',
            headers:{
                Authorization:localStorage.getItem('token')
            },
            success(res) {
                let list = res.data
                $('#selCategory').html(list.reduce((pre,v) =>  pre += `<option value="${v.id}">${v.name}</option>`,'<option value="">所有分类</option>'))
            }
        })
    }

    getAllList()

    $('#articleTbale').on('click','.delete',function() {
        layer.confirm('确定删除吗', {
            btn: ['确定', '取消'] //可以无限个按钮
          }, (index) =>{
            let id = $(this).parents('tr').data('id')
            console.log(id);
            $.ajax({
                url:'http://localhost:8080/api/v1/admin/article/delete',
                type:'post',
                data:{ id },
                headers:{
                    Authorization:localStorage.getItem('token')
                },
                success(res) {
                    console.log(res.msg);
                    getArticle_list()
                }
            })
            layer.close(index)
          });
    })  
    
    
})